﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PaperRenderingInterfaces;
using System.Xml.Serialization;
using System.IO;

namespace CDSParser
{
    internal class CDSUtilities
    {
        public static XMLSchema.collectionType FetchParsedCDSMARKXML(IWebAccess WebGetter, string renderAddress)
        {
            XMLSchema.collectionType parsedResult = null;
            string xmlResult = WebGetter.GetUriAsString(MARCXMLUriVersion(renderAddress));
            if (xmlResult != null)
            {

                ///
                /// Load it in as XML
                /// 

                XmlSerializer loader = new XmlSerializer(typeof(XMLSchema.collectionType));
                MemoryStream stream = new MemoryStream();
                byte[] data = Encoding.UTF8.GetBytes(xmlResult);
                stream.Write(data, 0, data.Length);
                stream.Position = 0;
                parsedResult = loader.Deserialize(stream) as XMLSchema.collectionType;

                if (parsedResult == null)
                {
                    parsedResult = new XMLSchema.collectionType();
                }
                if (parsedResult.record == null)
                {
                    parsedResult.record = new XMLSchema.recordType[0];
                }
            }
            return parsedResult;
        }

        public static XMLSchema.DC.collection FetchParsedCDSDCXML(IWebAccess WebGetter, string renderAddress)
        {
            XMLSchema.DC.collection parsedResult = null;
            string xmlResult = WebGetter.GetUriAsString(new Uri(renderAddress));
            if (xmlResult != null)
            {

                ///
                /// Load it in as XML
                /// 

                XmlSerializer loader = new XmlSerializer(typeof(XMLSchema.DC.collection));
                MemoryStream stream = new MemoryStream();
                byte[] data = Encoding.UTF8.GetBytes(xmlResult);
                stream.Write(data, 0, data.Length);
                stream.Position = 0;
                parsedResult = loader.Deserialize(stream) as XMLSchema.DC.collection;

                if (parsedResult == null)
                {
                    parsedResult = new XMLSchema.DC.collection();
                    parsedResult.Items = new XMLSchema.DC.dc[0];
                }
            }
            return parsedResult;
        }

        /// <summary>
        /// Returns a Uri that will ask for the MARC XML version of this search or whatever from CDS.
        /// </summary>
        /// <param name="renderAddress"></param>
        /// <returns></returns>
        private static Uri MARCXMLUriVersion(string renderAddress)
        {
            if (renderAddress.Contains("of=xm"))
            {
                return new Uri(renderAddress);
            }
            else
            {
                ///
                /// Does it already contain the output control sequence?
                /// 

                if (renderAddress.Contains("?of=") || renderAddress.Contains("&of="))
                {
                    ///
                    /// Replace whatever is there now.
                    /// 

                    int index = renderAddress.IndexOf("?of=");
                    if (index < 0)
                    {
                        index = renderAddress.IndexOf("&of=");
                    }
                    index += 1;
                    string oldDirective = renderAddress.Substring(index, 5);
                    string newRA = renderAddress;
                    newRA = newRA.Replace(oldDirective, "of=xm");
                    return new Uri(newRA);
                }
                else
                {
                    ///
                    /// Append the MARC XML request.
                    /// 

                    if (renderAddress.Contains("?"))
                    {
                        return new Uri(renderAddress + "&of=xm");
                    }
                    else
                    {
                        return new Uri(renderAddress + "?of=xm");
                    }
                }
            }
        }
    }
}
